﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Net;
using System.IO;


namespace Tooltipsgawe2
{
    
    public class TooltipPreview : ToolTip
    {
        private int value_width = 320;
		private int value_height = 240;
        private string[] tfile={" ","gawe2.gif"};
        public int node = 0;
        public string datas;
        
		public int TooltipEditWidth
		{
            get { return value_width; }
			set	{ value_width = value;  }
		}


		public int TooltipEditHeight
		{
            get { return value_height; }
			set	{ value_height = value;  }
		}


        public TooltipPreview()
        {
            this.Popup += new PopupEventHandler(this.OnPopup);            
        }
        

        public void PreviewWeb(Control control)
        {
            string data=this.GetToolTip(control);
            this.CheckDraw(data);
        }


        private void CheckDraw(string addlink)
        {
            string link = addlink + ".png";
            FileInfo fileInfo = new FileInfo(link);
            double fileKB = fileInfo.Length / 1024;

            DateTime timeNow = DateTime.Today;
            DateTime fileDown = File.GetLastWriteTime(link).Date;
            int result1 = DateTime.Compare(fileDown, timeNow);

            if (File.Exists(Path.Combine(Directory.GetCurrentDirectory(), link)))
            {
                if (fileKB == 0)
                {
                    node = 1;
                }
                else
                {
                    if (result1 < 0)
                    {
                        this.CaptureWeb(addlink);
                    }
                    else
                    {
                        node = 0;
                        tfile[node] = addlink + ".png";
                        this.CreateTooltip(addlink);
                    }
                }
            }

            else
            {
                this.CaptureWeb(addlink);
            }
        }


        private void CreateTooltip(string link)
        {
            this.OwnerDraw = true;
            this.Draw += new DrawToolTipEventHandler(this.OnDraw);
        }


        private void OnPopup(object sender, PopupEventArgs e)
        {
            e.ToolTipSize = new System.Drawing.Size(value_width, value_height);
        }


        private void OnDraw(object sender, DrawToolTipEventArgs e)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(tfile[node]);
            e.Graphics.DrawImage(img, 0, 0, value_width, value_height);   
        }


        private void CaptureWeb(String data)
        {
            WebClient client = new WebClient();
            string url = @"http://api.screenshotmachine.com/?key=7b4b18&size=E&url=http://"+data;
            client.DownloadFileAsync(new Uri(url), @""+data+".png");
        }
	
    }
    
}
